<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("userFunction.php");

$errMsg = "";
$id_user = sxREQUEST("id_user");
$uid = sxREQUEST("uid");
$pwd = sxREQUEST("pwd");
$name = sxREQUEST("name");
$last_name = sxREQUEST("last_name");
$authenticaz = sxREQUEST("authenticaz");
$mobile_phone = sxREQUEST("mobile_phone");
$e_mail = sxREQUEST("e_mail");
$phone = sxREQUEST("phone");

if(isset($_REQUEST["id"])){
	$id_user = $_REQUEST["id"];
	if(is_numeric($id_user)){
		$sSQL = "SELECT * FROM user WHERE id_user=$id_user";
		if($objSDAO->query($sSQL,"sel",array("contOnErr"=>true))){
			if($row = $objSDAO->fetch("sel")){
				$uid = $row["uid"];
				$pwd = '';
				$name = $row["name"];
				$last_name = $row["last_name"];
				$authenticaz = $row["authenticaz"];
				$mobile_phone = $row["mobile_phone"];
				$e_mail = $row["e_mail"];
				$phone = $row["phone"];
				
			}else{
				$errMsg .= _t("Impossibile caricare i dati dell'utente");
			}
			$sSQL = "SELECT p.* FROM profile p " .
					"INNER JOIN user_profile up ON p.id_profile=up.id_profile " .
					"WHERE up.id_user=$id_user ORDER BY p.description";
			if(!$objSDAO->query($sSQL,"selPro",array("contOnErr"=>true))){
				$errMsg .= $sSQL." - ".$objSDAO->error;
			}
		}else{
			$errMsg .= $sSQL." - ".$objSDAO->error;
		}
	}else{
		$errMsg .= _t("Il parametro 'id_user' non &egrave; corretto.");
	}
}

if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= userMod();	
}
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
	$errMsg .= userDel();	
}

//output HTML
$aOpz = array("tit2"=>_t("Aggiorna utente"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea =  document.getElementById("messArea");
		
		var obj =  document.getElementById("uid");
		if(obj.value == ""){
			errMsg += "<?php echo _t("Lo user name &egrave; obbligatorio")?>.<br>";
			obj.className = "inpt01";
			obj.focus();
		}else{
			obj.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"userMod.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"id_user\" id=\"id_user\" value=\"$id_user\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<input type=\"hidden\" name=\"uid\" id=\"uid\" value=\"".sxHE($uid)."\">");
//output comandi
$aCmd = array("ins"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati dell'utente"),
										"text"=>_t("salva"),
										"perm"=>"upduser"),
						"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1))
					);
echo(commandsHTML($aCmd));	
if($errMsg == ""){
	echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
				"<td class=\"titl01\" colspan=\"2\">"._t("Utente")."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">"._t("nome utente")."*</td>" .
				"<td class=\"text01\">".sxHE($uid)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">"._t("password")."*</td>" .
				"<td class=\"text01\"><input class=\"inpt01\" type=\"password\" name=\"pwd\" id=\"pwd\" maxlength=\"128\" size=\"32\" value=\"".sxHE($pwd)."\"></td>" .
			"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("nome")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"name\" id=\"name\" value=\"".sxHE($name)."\"></td>" .
	"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("cognome")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"last_name\" id=\"last_name\" value=\"".sxHE($last_name)."\"></td>" .
	"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("autenticreqtype")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"authenticaz\" id=\"authenticaz\"  size=\"32\" value=\"".sxHE($authenticaz)."\"></td>" .
	"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("telefono cellulare")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"mobile_phone\" id=\"mobile_phone\" value=\"".sxHE($mobile_phone)."\"></td>" .
	"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("e-mail")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"e_mail\" id=\"e_mail\"  size=\"32\" value=\"".sxHE($e_mail)."\"></td>" .
	"</tr>\n");
	echo("<tr>" .
		"<td class=\"labl01\">"._t("telefono fisso")."</td>" .
		"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"phone\" id=\"phone\" value=\"".sxHE($phone)."\"></td>" .
	"</tr>\n");	
	echo("</table>");
}

echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
